home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / Element.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  9.4 KB  |  394 lines

  1. package horst;
  2.  
  3. import horst.parser.HTMLDefs;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.util.Enumeration;
  7. import java.util.Hashtable;
  8. import java.util.Vector;
  9.  
  10. public class Element {
  11.    Element m_parent;
  12.    Element m_anchor;
  13.    HTMLDocument m_doc;
  14.    Vector m_children = new Vector();
  15.    Vector m_relatedElements = new Vector();
  16.    Vector m_origRelatedElements = new Vector();
  17.    Hashtable m_attributes = new Hashtable(10);
  18.    int m_type;
  19.    Font m_font;
  20.    boolean m_bDrawFocusBox;
  21.    boolean m_bHasFocus;
  22.    boolean m_bInPreformat = false;
  23.    Color m_focusColor;
  24.    int m_p0;
  25.    int m_p1;
  26.    int m_origP0;
  27.    int m_origP1;
  28.    int m_textbufferPosition;
  29.  
  30.    public Element(int type) {
  31.       this.m_focusColor = Color.red;
  32.       this.m_textbufferPosition = -1;
  33.       this.m_type = type;
  34.       this.m_bInPreformat = false;
  35.    }
  36.  
  37.    public Element(int type, int p0, int p1) {
  38.       this.m_focusColor = Color.red;
  39.       this.m_textbufferPosition = -1;
  40.       this.m_type = type;
  41.       this.m_p0 = p0;
  42.       this.m_p1 = p1;
  43.       this.m_origP0 = p0;
  44.       this.m_origP1 = p1;
  45.    }
  46.  
  47.    public Element(Element copy) {
  48.       this.m_focusColor = Color.red;
  49.       this.m_textbufferPosition = -1;
  50.       this.m_origP0 = copy.m_origP0;
  51.       this.m_origP1 = copy.m_origP1;
  52.       this.m_p0 = copy.m_p0;
  53.       this.m_p1 = copy.m_p1;
  54.       this.m_attributes = copy.getAttributes();
  55.       this.m_type = copy.getType();
  56.       this.m_font = copy.getFont();
  57.       this.m_bHasFocus = copy.getHasFocus();
  58.       this.m_focusColor = copy.getFocusColor();
  59.       this.m_bDrawFocusBox = copy.getDrawFocusBox();
  60.       this.m_doc = copy.getDocument();
  61.       this.m_bInPreformat = copy.m_bInPreformat;
  62.       this.m_anchor = copy.m_anchor;
  63.    }
  64.  
  65.    public void addChild(Element e) {
  66.       this.m_children.addElement(e);
  67.       e.m_parent = this;
  68.    }
  69.  
  70.    void addRelatedElement(Element e) {
  71.       this.m_relatedElements.addElement(e);
  72.       this.m_origRelatedElements.addElement(e);
  73.    }
  74.  
  75.    protected void flushResources() {
  76.       this.m_relatedElements.removeAllElements();
  77.       Element[] elems = new Element[this.m_children.size()];
  78.       this.m_children.copyInto(elems);
  79.  
  80.       for(int i = 0; i < elems.length; ++i) {
  81.          elems[i].flushResources();
  82.       }
  83.  
  84.    }
  85.  
  86.    public Object getAttribute(Object name) {
  87.       return this.m_attributes.get(name);
  88.    }
  89.  
  90.    public Hashtable getAttributes() {
  91.       return this.m_attributes;
  92.    }
  93.  
  94.    public char[] getCharData() {
  95.       if (this.m_p0 >= 0 && this.m_p1 >= this.m_p0) {
  96.          StringBuffer buf = this.getDocument().getTextBuffer();
  97.          if (buf != null) {
  98.             char[] data = new char[this.m_p1 - this.m_p0 + 1];
  99.             buf.getChars(this.m_p0, this.m_p1 + 1, data, 0);
  100.             return data;
  101.          }
  102.       }
  103.  
  104.       return null;
  105.    }
  106.  
  107.    public Vector getChildren() {
  108.       return this.m_children;
  109.    }
  110.  
  111.    public HTMLDocument getDocument() {
  112.       return this.m_doc;
  113.    }
  114.  
  115.    protected boolean getDrawFocusBox() {
  116.       return this.m_bDrawFocusBox;
  117.    }
  118.  
  119.    public Element getElementAt(int index) {
  120.       return (Element)this.m_children.elementAt(index);
  121.    }
  122.  
  123.    public int getElementCount() {
  124.       return this.m_children.size();
  125.    }
  126.  
  127.    public int getElementIndex(Element e) {
  128.       return this.m_children.indexOf(e);
  129.    }
  130.  
  131.    protected Color getFocusColor() {
  132.       return this.m_focusColor;
  133.    }
  134.  
  135.    public Font getFont() {
  136.       return this.m_font;
  137.    }
  138.  
  139.    protected boolean getHasFocus() {
  140.       return this.m_bHasFocus;
  141.    }
  142.  
  143.    public String getName() {
  144.       return HTMLDefs.getName(this.m_type);
  145.    }
  146.  
  147.    public Element getParent() {
  148.       return this.m_parent;
  149.    }
  150.  
  151.    Element[] getRelatedElements() {
  152.       Element[] elems = new Element[this.m_relatedElements.size()];
  153.       this.m_relatedElements.copyInto(elems);
  154.       return elems;
  155.    }
  156.  
  157.    public int getType() {
  158.       return this.m_type;
  159.    }
  160.  
  161.    public void insertElementAt(Element e, int idx) {
  162.       if (e != null && idx <= this.m_children.size() - 1) {
  163.          this.m_children.insertElementAt(e, idx);
  164.          e.m_parent = this;
  165.       }
  166.  
  167.       throw new IllegalArgumentException("Illegal Element insertion!");
  168.    }
  169.  
  170.    public boolean isAttributeDefined(Object key) {
  171.       return this.m_attributes.containsKey(key);
  172.    }
  173.  
  174.    public boolean isLink() {
  175.       return this.getAttribute("href") != null;
  176.    }
  177.  
  178.    protected void propagateDrawFocusBox(boolean bDraw) {
  179.       Enumeration e = this.m_relatedElements.elements();
  180.  
  181.       while(e.hasMoreElements()) {
  182.          Element elem = (Element)e.nextElement();
  183.          elem.setDrawFocusBox(bDraw);
  184.       }
  185.  
  186.    }
  187.  
  188.    protected void propagateFocus(boolean bFocus) {
  189.       this.m_bHasFocus = bFocus;
  190.       if (!bFocus) {
  191.          this.m_bDrawFocusBox = false;
  192.       }
  193.  
  194.       Enumeration e = this.m_relatedElements.elements();
  195.  
  196.       while(e.hasMoreElements()) {
  197.          Element elem = (Element)e.nextElement();
  198.          elem.setFocus(bFocus);
  199.          elem.setDrawFocusBox(this.m_bDrawFocusBox);
  200.       }
  201.  
  202.    }
  203.  
  204.    protected void propagateFocus(boolean bFocus, boolean bDrawFocusBox) {
  205.       this.m_bHasFocus = bFocus;
  206.       this.m_bDrawFocusBox = bDrawFocusBox;
  207.       Enumeration e = this.m_relatedElements.elements();
  208.  
  209.       while(e.hasMoreElements()) {
  210.          Element elem = (Element)e.nextElement();
  211.          elem.setFocus(bFocus);
  212.          elem.setDrawFocusBox(this.m_bDrawFocusBox);
  213.       }
  214.  
  215.    }
  216.  
  217.    public void removeAttribute(Object name) {
  218.       this.m_attributes.remove(name);
  219.    }
  220.  
  221.    public void replaceChildElement(Element oldChild, Element newChild) {
  222.       int idx = this.getElementIndex(oldChild);
  223.       if (idx != -1) {
  224.          this.m_children.removeElementAt(idx);
  225.          this.m_children.insertElementAt(newChild, idx);
  226.          newChild.m_parent = this;
  227.          oldChild.m_parent = null;
  228.       }
  229.  
  230.    }
  231.  
  232.    public void replaceChildElement(Element oldChild, Element[] newChildren) {
  233.       int idx = this.getElementIndex(oldChild);
  234.       if (idx != -1) {
  235.          oldChild.m_parent = null;
  236.          this.m_children.removeElementAt(idx);
  237.  
  238.          for(int i = 0; i < newChildren.length; ++i) {
  239.             this.m_children.insertElementAt(newChildren[i], idx + i);
  240.             newChildren[i].m_parent = this;
  241.          }
  242.       }
  243.  
  244.    }
  245.  
  246.    void replaceRelatedElement(Element oldElem, Element[] newElems) {
  247.       int sz = this.m_relatedElements.size();
  248.  
  249.       for(int i = 0; i < sz; ++i) {
  250.          Element elem = (Element)this.m_relatedElements.elementAt(i);
  251.          if (elem == oldElem) {
  252.             this.m_relatedElements.removeElementAt(i);
  253.  
  254.             for(int j = 0; j < newElems.length; ++j) {
  255.                this.m_relatedElements.addElement(newElems[j]);
  256.             }
  257.  
  258.             return;
  259.          }
  260.       }
  261.  
  262.    }
  263.  
  264.    void reset() {
  265.       this.m_p0 = this.m_origP0;
  266.       this.m_p1 = this.m_origP1;
  267.       this.m_relatedElements.removeAllElements();
  268.       Enumeration e = this.m_origRelatedElements.elements();
  269.  
  270.       while(e.hasMoreElements()) {
  271.          this.m_relatedElements.addElement(e.nextElement());
  272.       }
  273.  
  274.       Enumeration e = this.m_children.elements();
  275.  
  276.       while(e.hasMoreElements()) {
  277.          ((Element)e.nextElement()).reset();
  278.       }
  279.  
  280.    }
  281.  
  282.    public void setAttribute(Object name, Object value) {
  283.       this.m_attributes.put(name, value);
  284.    }
  285.  
  286.    public void setAttributes(Hashtable atts) {
  287.       this.m_attributes = atts;
  288.    }
  289.  
  290.    public void setDocument(HTMLDocument doc) {
  291.       this.m_doc = doc;
  292.       Element[] elems = new Element[this.m_children.size()];
  293.       this.m_children.copyInto(elems);
  294.  
  295.       for(int i = 0; i < elems.length; ++i) {
  296.          elems[i].setDocument(doc);
  297.       }
  298.  
  299.    }
  300.  
  301.    protected void setDrawFocusBox(boolean bDraw) {
  302.       this.m_bDrawFocusBox = bDraw;
  303.    }
  304.  
  305.    protected void setFocus(boolean bFocus) {
  306.       this.m_bHasFocus = bFocus;
  307.       if (!bFocus) {
  308.          this.m_bDrawFocusBox = false;
  309.       }
  310.  
  311.    }
  312.  
  313.    public void setFont(Font f) {
  314.       this.m_font = f;
  315.    }
  316.  
  317.    void setTextPointers(int p0, int p1) {
  318.       this.m_origP0 = p0;
  319.       this.m_origP1 = p1;
  320.       this.m_p0 = p0;
  321.       this.m_p1 = p1;
  322.    }
  323.  
  324.    public String toString() {
  325.       if (this.m_type == 9) {
  326.          String s = new String(this.getCharData());
  327.          boolean bBold = (this.m_font.getStyle() & 1) > 0;
  328.          if (bBold) {
  329.             System.out.println("Bold x" + s + "x");
  330.          }
  331.  
  332.          return s;
  333.       } else {
  334.          String name = this.getName();
  335.          if (name.length() == 0) {
  336.             return "";
  337.          } else {
  338.             String s = "<" + this.getName();
  339.             String attStr = "";
  340.             Enumeration e = this.m_attributes.keys();
  341.  
  342.             while(e.hasMoreElements()) {
  343.                String key = (String)e.nextElement();
  344.                if (HTMLAttributes.isHTMLAttribute(key)) {
  345.                   Object value = this.m_attributes.get(key);
  346.                   attStr = attStr + " " + key;
  347.                   if (value != null && value instanceof String && ((String)value).length() > 0) {
  348.                      attStr = attStr + "=\"" + value + "\"";
  349.                   } else {
  350.                      attStr = attStr + "=\"\"";
  351.                   }
  352.                }
  353.             }
  354.  
  355.             s = s + attStr;
  356.             s = s + ">";
  357.             return s;
  358.          }
  359.       }
  360.    }
  361.  
  362.    void trimLeft() {
  363.       StringBuffer buf = this.getDocument().getTextBuffer();
  364.       if (buf != null) {
  365.          while(this.m_p0 <= this.m_p1) {
  366.             char[] data = new char[this.m_p1 - this.m_p0 + 1];
  367.             buf.getChars(this.m_p0, this.m_p1 + 1, data, 0);
  368.             if (data[0] != ' ') {
  369.                break;
  370.             }
  371.  
  372.             ++this.m_p0;
  373.          }
  374.       }
  375.  
  376.    }
  377.  
  378.    void trimRight() {
  379.       StringBuffer buf = this.getDocument().getTextBuffer();
  380.       if (buf != null) {
  381.          while(this.m_p0 <= this.m_p1) {
  382.             char[] data = new char[this.m_p1 - this.m_p0 + 1];
  383.             buf.getChars(this.m_p0, this.m_p1 + 1, data, 0);
  384.             if (data[data.length - 1] != ' ') {
  385.                break;
  386.             }
  387.  
  388.             --this.m_p1;
  389.          }
  390.       }
  391.  
  392.    }
  393. }
  394.